# delimit ;

clear;
*version 13;
set matsize 400;
set more off;


log using "/Users/Jeff/Dropbox/TermLimits/Replication/Models/IndexEmpirical/Conditional/Mandates/PredictedValues.log", replace;
cd "/Users/Jeff/Dropbox/TermLimits/Replication/Models/IndexEmpirical/Conditional/Mandates";

**************************************************************;
*Author: Jeff Carter                                         *;
*Date: Tuesday, May 26, 2015                              *;
*Purpose: Simulating Predictions                *;
**************************************************************;



*********************************;
**      Peace to War            *;
*********************************;

use "/Users/Jeff/Dropbox/TermLimits/Replication/Models/IndexEmpirical/Conditional/Mandates/SimData.dta";

postutil clear;

postfile mypost prob_hat0 lo0 hi0 prob_hat1 lo1 hi1 prob_hat2 lo2 hi2 
                diff_hat0 diff_lo0 diff_hi0 diff_hat1 diff_lo1 diff_hi1
                prob_hat01 lo01 hi01 prob_hat11 lo11 hi11 prob_hat21 lo21 hi21
                diff_hat01 diff_lo01 diff_hi01 diff_hat11 diff_lo11 diff_hi11
                delta_hat delta_lo delta_hi delta_hat1 delta_lo1 delta_hi1 using PredictedValues, replace;

noisily display "start";


local a=-1 ;
while `a' < 4 { ;

{;


*tl2 milservice tl2_mil tl1 tl1_mil;
 
scalar h_Accountable =   0  ;
scalar h_LameDuck= 1  ;
scalar h_Civilian = 0;
scalar h_Military = 1;
scalar h_Constant = 1;
scalar h_OneTerm=1;
scalar h_TwoTerms=1;
scalar h_Parliament = 0;
scalar h_CivilWar = 0;
scalar h_GDPpc =  12589.35;
scalar h_Growth = 3.453132;
scalar h_MajorPower = 0;
scalar h_Borders = 2;
scalar h_Mobilization =  -.0008714 ;
scalar h_Time = 5; 
scalar h_Rivalry = 0;
scalar h_Female = 0;



generate x_betahat0 = MG_b1*h_Accountable
    + MG_b2*`a'
    + MG_b3*(h_Accountable*`a')
    + MG_b4*h_Accountable
    + MG_b5*(h_Accountable*`a')
    + MG_b6*h_Rivalry
    + MG_b7*h_Borders
    + MG_b8*h_Parliament
    + MG_b9*h_Female
    + MG_b10*h_Time
    + MG_b11*(h_Time^2)
    + MG_b12*(h_Time^3)
    + MG_b13*h_Constant;

 
generate x_betahat1 = MG_b1*h_OneTerm
    + MG_b2*`a'
    + MG_b3*(h_OneTerm*`a')
    + MG_b4*h_Accountable
    + MG_b5*(h_Accountable*`a')
    + MG_b6*h_Rivalry
    + MG_b7*h_Borders
    + MG_b8*h_Parliament
    + MG_b9*h_Female
    + MG_b10*h_Time
    + MG_b11*(h_Time^2)
    + MG_b12*(h_Time^3)
    + MG_b13*h_Constant;
 

generate x_betahat2 = MG_b1*h_Accountable
    + MG_b2*`a'
    + MG_b3*(h_Accountable*`a')
    + MG_b4*h_TwoTerms
    + MG_b5*(h_TwoTerms*`a')
    + MG_b6*h_Rivalry
    + MG_b7*h_Borders
    + MG_b8*h_Parliament
    + MG_b9*h_Female
    + MG_b10*h_Time
    + MG_b11*(h_Time^2)
    + MG_b12*(h_Time^3)
    + MG_b13*h_Constant;
 

 
gen prob0=1/(1+exp(-x_betahat0));
gen prob1=1/(1+exp(-x_betahat1));
gen prob2=1/(1+exp(-x_betahat2));

 
gen diff0=prob1-prob0 ;
gen diff1=prob2-prob0 ;

egen probhat0=mean(prob0) ;
egen probhat1=mean(prob1) ;
egen probhat2=mean(prob2) ;

egen diffhat0=mean(diff0) ;
egen diffhat1=mean(diff1) ;



generate x_betahat01 =   MG_b1*h_Accountable
    + MG_b2*`a'
    + MG_b3*(h_Accountable*`a')
    + MG_b4*h_Accountable
    + MG_b5*(h_Accountable*`a')
    + MG_b6*h_Rivalry
    + MG_b7*h_Borders
    + MG_b8*h_Parliament
    + MG_b9*h_Female
    + MG_b10*h_Time
    + MG_b11*(h_Time^2)
    + MG_b12*(h_Time^3)
    + MG_b13*h_Constant;

 
generate x_betahat11 =   MG_b1*h_OneTerm
    + MG_b2*`a'
    + MG_b3*(h_OneTerm*`a')
    + MG_b4*h_Accountable
    + MG_b5*(h_Accountable*`a')
    + MG_b6*h_Rivalry
    + MG_b7*h_Borders
    + MG_b8*h_Parliament
    + MG_b9*h_Female
    + MG_b10*h_Time
    + MG_b11*(h_Time^2)
    + MG_b12*(h_Time^3)
    + MG_b13*h_Constant;
 

 generate x_betahat21 =   MG_b1*h_Accountable
    + MG_b2*`a'
    + MG_b3*(h_Accountable*`a')
    + MG_b4*h_TwoTerms
    + MG_b5*(h_TwoTerms*`a')
    + MG_b6*h_Rivalry
    + MG_b7*h_Borders
    + MG_b8*h_Parliament
    + MG_b9*h_Female
    + MG_b10*h_Time
    + MG_b11*(h_Time^2)
    + MG_b12*(h_Time^3)
    + MG_b13*h_Constant;
 
 

gen prob01=1/(1+exp(-x_betahat01));
gen prob11=1/(1+exp(-x_betahat11));
gen prob21=1/(1+exp(-x_betahat21));

gen diff01=prob11-prob01 ;
gen diff11=prob21-prob01 ;

egen probhat01=mean(prob01) ;
egen probhat11=mean(prob11) ;
egen probhat21=mean(prob21) ;

egen diffhat01=mean(diff01) ;
egen diffhat11=mean(diff11) ;



gen delta=diff01-diff0;
gen delta1=diff11-diff1;

egen deltahat=mean(delta);
egen deltahat1=mean(delta1);




tempname prob_hat0 lo0 hi0 prob_hat1 lo1 hi1 prob_hat2 lo2 hi2
         diff_hat0 diff_lo0 diff_hi0 diff_hat1 diff_lo1 diff_hi1
         prob_hat01 lo01 hi01 prob_hat11 lo11 hi11 prob_hat21 lo21 hi21
         diff_hat01 diff_lo01 diff_hi01 diff_hat11 diff_lo11 diff_hi11
         delta_hat delta_lo delta_hi  delta_hat1 delta_lo1 delta_hi1;

    _pctile prob0, p(2.5,97.5) ;
    scalar `lo0'=r(r1) ;
    scalar `hi0'=r(r2) ;

    _pctile prob1, p(2.5,97.5) ;
    scalar `lo1'=r(r1) ;
    scalar `hi1'=r(r2) ;

    _pctile prob2, p(2.5,97.5) ;
    scalar `lo2'=r(r1) ;
    scalar `hi2'=r(r2) ;
    
    _pctile diff0, p(2.5,97.5) ;
    scalar `diff_lo0'= r(r1) ;
    scalar `diff_hi0'=r(r2) ;

    _pctile diff1, p(2.5,97.5) ;
    scalar `diff_lo1'= r(r1) ;
    scalar `diff_hi1'=r(r2) ;


    scalar `prob_hat0'=probhat0 ;
    scalar `prob_hat1'=probhat1 ;
    scalar `prob_hat2'=probhat2 ;
    scalar `diff_hat0'=diffhat0 ;
    scalar `diff_hat1'=diffhat1 ;


       _pctile prob01, p(2.5,97.5) ;
    scalar `lo01'=r(r1) ;
    scalar `hi01'=r(r2) ;

    _pctile prob11, p(2.5,97.5) ;
    scalar `lo11'=r(r1) ;
    scalar `hi11'=r(r2) ;

    _pctile prob21, p(2.5,97.5) ;
    scalar `lo21'=r(r1) ;
    scalar `hi21'=r(r2) ;

    _pctile diff01, p(2.5,97.5) ;
    scalar `diff_lo01'= r(r1) ;
    scalar `diff_hi01'=r(r2) ;

    _pctile diff11, p(2.5,97.5) ;
    scalar `diff_lo11'= r(r1) ;
    scalar `diff_hi11'=r(r2) ;


    scalar `prob_hat01'=probhat01 ;
    scalar `prob_hat11'=probhat11 ;
    scalar `prob_hat21'=probhat21 ;
    scalar `diff_hat01'=diffhat01 ;
    scalar `diff_hat11'=diffhat11 ;


    _pctile delta, p(2.5,97.5) ;
    scalar `delta_lo'= r(r1) ;
    scalar `delta_hi'=r(r2) ;


    _pctile delta1, p(2.5,97.5) ;
    scalar `delta_lo1'= r(r1) ;
    scalar `delta_hi1'=r(r2) ;

    scalar `delta_hat'=deltahat;
    scalar `delta_hat1'=deltahat1;


    post mypost (`prob_hat0') (`lo0') (`hi0') (`prob_hat1') (`lo1') (`hi1') (`prob_hat2') (`lo2') (`hi2')
                (`diff_hat0') (`diff_lo0') (`diff_hi0') (`diff_hat1') (`diff_lo1') (`diff_hi1')
                (`prob_hat01') (`lo01') (`hi01') (`prob_hat11') (`lo11') (`hi11')  (`prob_hat21') (`lo21') (`hi21')
                (`diff_hat01') (`diff_lo01') (`diff_hi01') (`diff_hat11') (`diff_lo11') (`diff_hi11')
                (`delta_hat') (`delta_lo') (`delta_hi') (`delta_hat1') (`delta_lo1') (`delta_hi1')

    ;

    } ;

    drop    x_betahat0 x_betahat1 x_betahat2
            prob0 prob1 prob2 
            diff0 diff1
            probhat0 probhat1 probhat2
            diffhat0 diffhat1
            x_betahat01 x_betahat11 x_betahat21
            prob01 prob11 prob21
            diff01 diff11
            probhat01 probhat11 probhat21
            diffhat01 diffhat11
            delta deltahat delta1 deltahat1;

    local a=`a'+ 1 ;

    display "." _c ;
    } ;
display "" ; postclose mypost ;



use PredictedValues, clear;

gen ruler = _n;
sum;

saveold PredictedValues, replace;

log close;


twoway (rline diff_lo0 diff_hi0 ruler);
